package com.arcvideo.acsplayer.widget;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;
import com.arcvideo.acsplayer.app.AcsPlayerApplication;
import com.arcvideo.acsplayer.data.AcsPlayerData;
import com.arcvideo.acsplayer.mode.InputVideoInfo;
import com.arcvideo.acsplayer.utils.DbUtil;

public class HistroyListView extends FrameLayout implements android.widget.AdapterView.OnItemClickListener {
	private Context mContext = null;
	private ArrayList<InputVideoInfo> mUrlList = null;
	private HistoryAdapter mHistroyAdapter = null;
	private HistroyInterface mHistroyInterface = null;
	private ListView mHistroyListView = null;

	private File m_fileUrl = null;
	private String m_strSpitChar = "\r\n";

	/**
	 * 回调接口
	 *
	 * @author skl5250
	 *
	 */
	public interface HistroyInterface {
		public void clickItem(InputVideoInfo videoInfo);
	}

	/**
	 * 构造函数
	 *
	 * @param context
	 */
	public HistroyListView(Context context) {
		super(context);
		mContext = context;
		LayoutInflater.from(context).inflate(R.layout.histroy_list, this);
		mHistroyListView = (ListView) findViewById(R.id.list);
	}

	/**
	 * 构造函数
	 *
	 * @param context
	 * @param attributeSet
	 */
	public HistroyListView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
		LayoutInflater.from(context).inflate(R.layout.histroy_list, this);
		mHistroyListView = (ListView) findViewById(R.id.list);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();

	}

	/**
	 * 初始化
	 *
	 * @param histroyInterface
	 */
	public void initHistoryList(HistroyInterface histroyInterface) {
		mUrlList = new ArrayList<InputVideoInfo>();
		getUrlList();
		mHistroyInterface = histroyInterface;
		if (null != mUrlList) {
			mHistroyAdapter = new HistoryAdapter(mContext, mUrlList);
			mHistroyListView.setAdapter(mHistroyAdapter);
			mHistroyListView.setOnItemClickListener(this);
		}
	}

	/**
	 * 更新历史记录
	 */
	public void updateHistoryList() {
		getUrlList();
		mHistroyAdapter.updateList(mUrlList);
	}

	/**
	 * 得到历史记录列表
	 */
	private void getUrlList() {

		//InputVideoInfo videoInfo1 = new InputVideoInfo();
		//videoInfo1.setDisplayName("hyrz");
		//videoInfo1.setPath("http://172.17.230.220/hzhang/hyrz/hyrz.m3u8");
		//mUrlList.add(videoInfo1);
		//ArcPlayerApplication.getInstance().getDbHelper().addInputVideoInfo(videoInfo1);

		mUrlList = AcsPlayerApplication.getInstance().getDbHelper().getInputVideoInfoList();
		AcsPlayerData.getInstance().setmInputVideoInfoList(mUrlList);
	}

	/**
	 * 保存历史列表
	 */
	private void saveUrlList() {
		if (null != m_fileUrl) {
			if (m_fileUrl.exists())
				m_fileUrl.delete();
			try {
				m_fileUrl.createNewFile();
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < mUrlList.size(); i++) {
					sb.append(mUrlList.get(i));
					sb.append(m_strSpitChar);
				}
				FileOutputStream os = new FileOutputStream(m_fileUrl);
				System.out.println(sb.toString());
				os.write(sb.toString().getBytes());
				os.flush();
				os.close();
				os = null;
				m_fileUrl = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 添加新的历史记录
	 *
	 * @param strNewUrl
	 */
	public void addNewUrl(String strNewUrl) {
		boolean bHaveSame = false;
		for (int i = 0; i < mUrlList.size(); i++) {
			if (strNewUrl.compareToIgnoreCase(mUrlList.get(i).getPath()) == 0) {
				bHaveSame = true;
				break;
			}
		}

		if (!bHaveSame) {
			InputVideoInfo videoInfo = new InputVideoInfo();
			videoInfo.setDisplayName(strNewUrl.substring(strNewUrl.lastIndexOf("/") + 1));
			videoInfo.setPath(strNewUrl);
			mUrlList.add(videoInfo);
			saveUrlList();
		}
	}

	/**
	 * 历史列表
	 *
	 * @author skl5250
	 *
	 */
	public class HistoryAdapter extends BaseAdapter {

		/**
		 * 历史URL记录
		 */
		private ArrayList<InputVideoInfo> urlList = new ArrayList<InputVideoInfo>();

		/**
		 * 上下文
		 */
		private Context context;

		/**
		 * 构造函数
		 */
		public HistoryAdapter(Context context, ArrayList<InputVideoInfo> urlList) {
			this.context = context;
			this.urlList = urlList;
		}

		/**
		 * 更新列表
		 */
		public void updateList(ArrayList<InputVideoInfo> urlList) {
			this.urlList = urlList;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return this.urlList.size();
		}

		@Override
		public Object getItem(int position) {
			return this.urlList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			final InputVideoInfo videoInfo = this.urlList.get(position);
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.item_url, null);
				viewHolder.urlText = (TextView) convertView.findViewById(R.id.url);

				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.urlText.setText(videoInfo.getDisplayName());
			return convertView;
		}

		private class ViewHolder {
			TextView urlText;
		}
	}

	/**
	 * 列表按鍵事件
	 */
	public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
		if (null != mHistroyInterface) {
			mHistroyInterface.clickItem(mUrlList.get(position));
		}
	}
}